<?php


namespace App\Http\Controllers\Manage\System\Dictionary;


use App\Http\Controllers\Controller;
use App\Http\Requests\Manage\System\Dictionary\Basics\StoreRequest;
use App\Http\Requests\Manage\System\Dictionary\Basics\UpdateRequest;
use App\Http\Searchs\Manage\System\Dictionary\BasicsSearch;
use App\Repositories\Dictionary;

class BasicsController extends Controller
{
    /**
     * @var Dictionary
     */
    private $dictionary;

    /**
     * IndexController constructor.
     * @param Dictionary $dictionary
     */
    public function __construct(Dictionary $dictionary)
    {
        $this->dictionary = $dictionary;
    }

    /**
     * @param BasicsSearch $search
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(BasicsSearch $search)
    {
        return $this->response(
            $this->dictionary->paginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            $this->dictionary->create($request->only([
                'code', 'name', 'element'
            ]))
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit($id)
    {
        return $this->response(
            $this->dictionary->find($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            $this->dictionary->update($id, $request->only([
                'code', 'name', 'element'
            ]))
        );
    }
}
